from selenium import webdriver
from Automation_frame.parsing.ParserXlrd import Parser
from Automation_frame.parsing.Test_cases import Mycase
from Automation_frame.Keylib.Keylib_ui import KeyLib_ui
import threading,csv
class runner(object):
    def __init__(self):
        self.driver=None
        self.keylib = None
        self.Mycase=Mycase()
        self.strlist=[]
    def get_browser(self,browser_type='firefox'):
        if browser_type.lower()=='firefox':
            self.driver=webdriver.Firefox()
        elif browser_type.lower()=='chrome':
            self.driver=webdriver.Chrome()
        else:
            print('你输入的浏览器类型不支持')
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        return self.driver
    def begin(self, path, sheetname, mark, Automation=None):
        global a
        self.data=Parser(path)
        self.data.get_sheet(sheetname)
        for case in self.data.Parser_xl():
            # self.strlist = []
            list=[]
            # rlts=[]
            testdata=case.testdata
            if int(case.mark)==int(mark):
                with open(r'../test_data/result_data.txt', mode='w+', newline='') as file:
                    file.truncate()
                begin_text=f'开始执行用例：{case.caseid}，用例标题：{case.descrition}'
                self.strlist.append(begin_text)
                self.writer_file((begin_text))
                print(begin_text)
                self.driver = self.get_browser()  # 实例化浏览器
                self.keylib = KeyLib_ui(self.driver)
                # rdict = self.Collector.get_data(testdata) # 采集用例数据，把tc对象存放到采集器里面
                for i in testdata:
                    key=i['key']
                    object=i['object']
                    func = getattr(self.keylib, key)
                    if len(i)==2:
                        a=func(object)
                    elif len(i)==3:
                        a=func(object,i['param'])
                        # self.Mycase.get_case()
                    list.append(a)
                # rlts.append(a)
                # self.Collector.get_result(rdict,rlts)
                operation_text=[case,case.descrition,list]
                self.strlist.append(operation_text)
                self.strlist.append(f'用例{case.caseid}执行结束')
                self.strlist.append('**************************用例分割线****************************')


                self.writer_file(operation_text)
                self.writer_file(f'用例{case.caseid}执行结束')
                self.writer_file('----------------------用例分割线-----------------------------')

                print(operation_text)
                print(f'用例{case.caseid}执行结束')
                print('----------------------用例分割线-----------------------------')
                self.driver.close()  #关闭浏览器
            else:
                self.strlist.append(f'跳过用例{case.caseid}')
                self.writer_file(f'跳过用例{case.caseid}')
                print(f'跳过用例{case.caseid}')
            # return self.strlist
        # self.applist.append(self.strlist)
        return self.strlist
            # self
            # self.Collector.print_rlt()
            # self.applist.append(self.strlist)
            # mainwindows = QMainWindow()
            # self.Ui_Automation.setupUi(Automation)
            # self.Ui_Automation.text_result.append('1111')
        # return self.strlist
    # def __del__(self):
    #     self.strlist.append(f'所有用例执行完成')
    #     print(f'所有用例执行完成')
    #     self.writer_file(self.strlist)
    #     return self.strlist
    def writer_file(self,date):
        with open(r'../test_data/result_data.txt',mode='w+',newline='') as file:
            csv.writer(file).writerow(date)
    def print_text(self):
        return self.strlist

if __name__ == '__main__':
    runner().begin(path=r'../test_data/testdata998.xls',sheetname='KDT',mark=1)
    # a=runner().strlist
    # A=runner().print()
    # a=threading.Thread(target=runner().print).start()
